package com.example.h3x.androidmalware.View.Dialogs;


import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.util.DisplayMetrics;
import android.util.Log;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by h3x on 23.02.18.
 * full example on https://github.com/h3xb0y/AndroidTaker
 */

public class HardwareDialog extends DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        String out = "";
        out+="Screen size: "+toString().valueOf(screenSize())+"\n";
        out+="battery capacity: "+toString().valueOf(getBatteryCapacity())+"\n";
        out+="mobile: "+android.os.Build.MODEL+"\n";
        out+="****************\n";
        out+=CPU_info()+"\n";
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Hardware info");
        builder.setMessage(out);
        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
            }
        });
        return builder.create();
    }

    public double screenSize() {

        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        double x = Math.pow(dm.widthPixels / dm.xdpi, 2);
        double y = Math.pow(dm.heightPixels / dm.ydpi, 2);
        double screenInches = Math.sqrt(x + y); Log.d("debug", "Screen inches : " + screenInches);

        return truncate(screenInches, 1);
    }

    public static double truncate(double c, int n){
        int d = (int) Math.pow(10, n);
        double r = (Math.rint(c*d)/d);
        return r;
    }
    public Double getBatteryCapacity(){

        Object powerProfile = null;
        Double batteryCapacity = 0.0;
        final String POWER_PROFILE_CLASS = "com.android.internal.os.PowerProfile";

        try{
            powerProfile = Class.forName(POWER_PROFILE_CLASS)
                    .getConstructor(Context.class).newInstance(this);
        }catch (Exception e){
            e.printStackTrace();
        }

        try{
            batteryCapacity = (Double) Class.forName(POWER_PROFILE_CLASS)
                    .getMethod("getAveragePower", java.lang.String.class)
                    .invoke(powerProfile, "battery.capacity");
        }catch (Exception e) {
            e.printStackTrace();
        }

        return batteryCapacity;
    }
    private String CPU_info()
    {
        ProcessBuilder cmd;
        String result="";

        try{
            String[] args = {"/system/bin/cat", "/proc/cpuinfo"};
            cmd = new ProcessBuilder(args);

            Process process = cmd.start();
            InputStream data = process.getInputStream();
            byte[] re = new byte[1024];
            while(data.read(re) != -1){
                System.out.println(new String(re));
                result = result + new String(re);
            }
            data.close();} catch(IOException ex){ex.printStackTrace();
        }
        return result;
    }
}
